package com.lishu.carsafe.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.lishu.carsafe.R;
import com.lishu.carsafe.adapter.MonitorWarnAdapter;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.MonitorActiveInfoModel;
import com.lishu.carsafe.model.MonitorActiveInfoModel.MonitorActiveInfo;
import com.lishu.carsafe.model.MonitorBaseInfoModel;
import com.lishu.carsafe.model.entity.Device;
import com.lishu.carsafe.model.entity.LineId;
import com.lishu.carsafe.model.entity.LineVersion;
import com.lishu.carsafe.model.entity.Lonlat;
import com.lishu.carsafe.model.entity.VehicleDetail;
import com.lishu.carsafe.utils.StringUtil;
import com.lishu.carsafe.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * describe:
 *
 * @author:ZhangWei(张伟) date:2020/6/5
 * time:12:30
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class MonitorRealTimeActivity extends BaseMapActivity {
    //监控实体类
    public static final String MONITOR_STATE = "monitor_state";
    public static final String MONITOR_VEHICLE_ID = "monitor_vehicle_id";
    private int state;
    private String vehicleId;

    //车辆信息
    private TextView tv_monitor_car,tv_monitor_car_line;
    //车辆当前行驶信息
    private TextView tv_monitor_run,tv_monitor_direct,tv_monitor_speed,tv_monitor_elect;
    private TextView tv_monitor_volt,tv_monitor_flow,tv_monitor_break,tv_monitor_gau;

    //车载主机 惯导 视频监控
    private ImageView iv_monitor_host,iv_monitor_ins,iv_monitor_video;
    //多媒体终端 激光雷达 毫米波雷达
    private ImageView iv_monitor_media,iv_monitor_radar,iv_monitor_wave;

    //告警信息
    private LinearLayout ll_monitor_warn;

    //告警信息 列表
    private RecyclerView rv_view;
    private MonitorWarnAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor_real_time);

        // 申请动态权限
        requestPermission();

        initView();
        initData();
        initEven();
    }

    private void initEven() {
        ll_monitor_warn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MonitorRealTimeActivity.this,MonitorWarnActivity.class);
                intent.putExtra(MONITOR_VEHICLE_ID,vehicleId);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        state =  getIntent().getIntExtra(MONITOR_STATE,0);
        vehicleId =  getIntent().getStringExtra(MONITOR_VEHICLE_ID);

        setTopTitle("实时监测");
        //开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        initMapLocation();
    }

    public void initView() {
        super.initView();
        //获取地图控件引用
        tv_monitor_car = findViewById(R.id.tv_monitor_car);
        tv_monitor_car_line = findViewById(R.id.tv_monitor_car_line);
        tv_monitor_run = findViewById(R.id.tv_monitor_run);
        tv_monitor_direct = findViewById(R.id.tv_monitor_direct);
        tv_monitor_speed = findViewById(R.id.tv_monitor_speed);
        tv_monitor_elect = findViewById(R.id.tv_monitor_elect);
        tv_monitor_volt = findViewById(R.id.tv_monitor_volt);
        tv_monitor_flow = findViewById(R.id.tv_monitor_flow);
        tv_monitor_break = findViewById(R.id.tv_monitor_break);
        tv_monitor_gau = findViewById(R.id.tv_monitor_gau);
        iv_monitor_host = findViewById(R.id.iv_monitor_host);
        iv_monitor_ins = findViewById(R.id.iv_monitor_ins);
        iv_monitor_video = findViewById(R.id.iv_monitor_video);
        iv_monitor_media = findViewById(R.id.iv_monitor_media);
        iv_monitor_radar = findViewById(R.id.iv_monitor_radar);
        iv_monitor_wave = findViewById(R.id.iv_monitor_wave);
        ll_monitor_warn = findViewById(R.id.ll_monitor_warn);
        rv_view = findViewById(R.id.rv_view);

        rv_view.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MonitorWarnAdapter(this);
        rv_view.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getLinkDetail();
    }


    /**
     * 获取路线详情
     */
    private void getLinkDetail() {
        showAlways("正在加载数据，请稍后");
        HttpLoader.getMonitorActiveInfo(vehicleId, new HttpBase.IResponseListener() {
            @Override
            public void onResponse(BaseModel model) {
                hide();
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }
                MonitorActiveInfoModel detail = (MonitorActiveInfoModel) model;
                //获取车辆数据 更新布局
                refresh(detail.getData());
            }
        });
    }

    private void getMonitorBaseInfo(int lineId){
        //请求线路
        showAlways("正在加载线路数据，请稍后");
        HttpLoader.getMonitorBaseInfo(vehicleId, lineId+"",new HttpBase.IResponseListener() {
            @Override
            public void onResponse(BaseModel model) {
                hide();
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }
                MonitorBaseInfoModel detail = (MonitorBaseInfoModel) model;
                //更新站点信息
                drawPoints(detail.getData().getLineId().getLonlats());
            }
        });
    }

    //刷新数据
    private void refresh(MonitorActiveInfo info) {
        LineVersion lineVersion = info.getLineId();
        tv_monitor_car_line.setText(lineVersion.getName());//悠湖一号线

        VehicleDetail detail = info.getVehicle();
        tv_monitor_car.setText(StringUtil.getEmptyOrNull(detail.getName())); //未来一号车
        tv_monitor_run.setText(StringUtil.getEmptyOrNull(detail.getState()));//运行状态
        tv_monitor_direct.setText(StringUtil.getEmptyOrNull(detail.getDirection()));//行驶方向
        tv_monitor_speed.setText(StringUtil.getEmptyOrNull(detail.getSpeed()));//当前车速
        tv_monitor_elect.setText(StringUtil.getEmptyOrNull(detail.getBattery()));//电池电量

        tv_monitor_volt.setText(StringUtil.getEmptyOrNull(detail.getVoltage()));//当前电压
        tv_monitor_flow.setText(StringUtil.getEmptyOrNull(detail.getElectricity()));//当前电流
        tv_monitor_break.setText(StringUtil.getEmptyOrNull(detail.getBrake()));//刹车当前量
        tv_monitor_gau.setText(StringUtil.getEmptyOrNull(detail.getAccelerator()));//油门当前量

        //先判断是离线还是在线 然后去判断是否异常
        if(state == 1){
            //在线的情况下 判断为是否异常
            changeHostImage(info);
        }else{
            //离线情况全部都是灰色
        }
        //开始请求线路
        getMonitorBaseInfo(info.getLineId().getId());
        adapter.setList(info.getAlarmInfo());
    }

    private void changeHostImage(MonitorActiveInfo info){
        Device device = info.getDevice();
        if(device.getHost() == 0){
            //蓝色
            iv_monitor_host.setImageResource(R.mipmap.icon_monitor_host_line);
        }else{
            //红色
            iv_monitor_host.setImageResource(R.mipmap.icon_monitor_host_warn);
        }
        if(device.getGPSSideMachine() == 0){
            //蓝色
            iv_monitor_ins.setImageResource(R.mipmap.icon_monitor_ins_line);
        }else{
            //红色
            iv_monitor_ins.setImageResource(R.mipmap.icon_monitor_ins_warn);
        }
        if(device.getVideoHost() == 0){
            //蓝色
            iv_monitor_video.setImageResource(R.mipmap.icon_monitor_line);
        }else{
            //红色
            iv_monitor_video.setImageResource(R.mipmap.icon_monitor_warn);
        }

        if(device.getMediaTM() == 0){
            //蓝色
            iv_monitor_media.setImageResource(R.mipmap.icon_monitor_dedia_line);
        }else{
            //红色
            iv_monitor_media.setImageResource(R.mipmap.icon_monitor_dedia_warn);
        }
        if(device.getLidar16() == 0){
            //蓝色
            iv_monitor_radar.setImageResource(R.mipmap.icon_monitor_radar_line);
        }else{
            //红色
            iv_monitor_radar.setImageResource(R.mipmap.icon_monitor_radar_warn);
        }
        if(device.getMillWaveRada() == 0){
            //蓝色
            iv_monitor_wave.setImageResource(R.mipmap.icon_monitor_wave_line);
        }else{
            //红色
            iv_monitor_wave.setImageResource(R.mipmap.icon_monitor_wave_warn);
        }

    }
}
